package cn.zhangxinyu.org.service.impl;

import cn.zhangxinyu.basic.util.PageList;
import cn.zhangxinyu.org.domain.Department;
import cn.zhangxinyu.org.mapper.DepartmentMapper;
import cn.zhangxinyu.org.query.DepartmentQuery;
import cn.zhangxinyu.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
//事物
@Transactional
public class DepartmentServiceImpl implements IDepartmentService {

    //注入Mapper层
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional(readOnly = true)
    public Department loadById(Long id) {

        return departmentMapper.loadById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Department> loadAll() {

        return departmentMapper.loadAll();
    }


    @Override
    public void save(Department department) {
        departmentMapper.save(department);
    }

    @Override
    public void remove(Long id) {
        departmentMapper.remove(id);
    }

    @Override
    public void update(Department department) {
        departmentMapper.update(department);
    }


    @Override
    public PageList<Department> queryData(DepartmentQuery query) {
        //1.查询条数
        Long count = departmentMapper.queryCount(query);
        //2.如果一条数据都没有则返回默认值
        if(count==0){
            return new PageList<>();
        }
        //3.查询数据
        List<Department> data = departmentMapper.queryData(query);
        //4.返回数据
        return new PageList<>(count,data);
    }
}
